package org.zhouhe.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhouhe.dao.HibernateDao;
import org.zhouhe.dao.MybatisDao;
import org.zhouhe.model.PaginationBean;

@Service
@SuppressWarnings("unchecked")
public class BaseService {

	@Autowired
	private HibernateDao hibernateDao;
	
	@Autowired
	private MybatisDao mybatisDao;

	/** Hibernate method **/
	/**
	 * Hibernate save
	 * @param bean 
	 * @return Serializable PK
	 */
	public Serializable save(Object bean){
		return hibernateDao.getHibernateSessionFactory().getCurrentSession().save(bean);
	}
	/**
	 * Hibernate update
	 * @param bean 
	 */
	public void update(Object bean){
		hibernateDao.getHibernateSessionFactory().getCurrentSession().update(bean);
	}
	/**
	 * Hibernate delete
	 * @param bean 
	 */
	public void delete(Object bean){
		hibernateDao.getHibernateSessionFactory().getCurrentSession().delete(bean);
	}
	/**
	 * Hibernate saveOrUpdate
	 * @param bean 
	 */
	public void saveOrUpdate(Object bean){
		hibernateDao.getHibernateSessionFactory().getCurrentSession().saveOrUpdate(bean);
	}
	/**
	 * Hibernate get
	 * @param clazz
	 * @param id
	 * @return
	 */
	public <T> T get(Class<T> clazz, Serializable id){
		return (T) hibernateDao.getHibernateSessionFactory().getCurrentSession().get(clazz, id);
	}
	/**
	 * Mybatis分页查询
	 * 
	 * @作者： zhouhe
	 * @param sqlId Mybatis中配置的SQL-ID
	 * @param page 要查询的是第几页
	 * @param rows 每页显示记录数
	 * @param paramObject 查询参数
	 * @return PaginationBean
	 */
	public PaginationBean selectListByPage(String sqlId, Integer page, Integer rows, Object paramObject) {
		return mybatisDao.getListByPage(sqlId, page, rows, paramObject);
	}
	/**
	 * Mybatis，查询一条记录
	 * @param sqlId
	 * @param param
	 * @return
	 */
	public <T> T selectOne(String sqlId, Object param){
		return mybatisDao.getSqlSession().selectOne(sqlId, param);
	}
	
	/**
	 * Mybatis查询，返回结果列表（List）
	 * @param sqlId
	 * @param param
	 * @return
	 */
	public <T> List<T> selectList(String sqlId, Object param){
		return mybatisDao.getSqlSession().selectList(sqlId, param);
	}
	
	/**
	 * Mybatis执行SQL语句
	 * @param sqlId
	 * @param param
	 * @return 处理的记录数量
	 */
	public int execute(String sqlId, Object param){
		return mybatisDao.getSqlSession().update(sqlId, param);
	}
}
